<template>
  <div class="login-container">
    <van-nav-bar
      title="登录"
      class="page-nav-bar"
    >
    <template #left>
      <van-icon class="cross-text" name="cross" size="18" @click="$router.go(-1)" />
    </template>
    </van-nav-bar>

    <van-form ref="loginForm" @submit="onSubmit">
      <van-field
        v-model="loginObj.mobile"
        name="mobile"
        placeholder="请输入手机号码"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="loginObj.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down
            :time="5 * 1000"
            format="ss s"
            v-if="isShowCountDown"
            @finish="isShowCountDown = false"/>
          <van-button 
            v-else
            class="send-sms-btn" 
            round 
            size="small"
            native-type="button" 
            @click="sendSmsFn"
            type="primary">
            发送验证码
          </van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import {login, sendSms} from '@/api/user'

export default {
  name: 'login',
  data() {
    return {
      loginObj: {
        mobile: '',
        code: ''
      },
      userFormRules: {
        mobile: [{
          required: true,
          message: '手机号不能为空'
        }, {
          pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/,
          message: '手机号格式错误'
        }],
        code: [{
          required: true,
          message: '验证码不能为空'
        }, {
          pattern: /^\d{6}$/,
          message: '验证码格式错误'
        }]
      },
      isShowCountDown: false
    }
  },
  methods: {
    async onSubmit() {
   
      this.$toast.loading({
        duration: 0,
        forbidClick: true,
        message: '登录中...'
      })
      try{
        console.log(this.loginObj)
        const {data} = await login(this.loginObj)

        console.log(data)
        this.$toast.success('登录成功')

        this.$store.commit('setUser', data.data)

        this.$router.go(-1)
      } catch(err) {
        this.$toast.fail('登录失败,请稍后重试')
      }
    },
    async sendSmsFn() {
      try {
        await this.$refs.loginForm.validate('mobile')
      } catch(err) {
        return this.$toast.fail('请输入手机号')
      }
    
      this.isShowCountDown = true

      try {
        await sendSms(this.loginObj.mobile)
        this.$toast('发送验证码成功')
      } catch(err) {
        this.isShowCountDown = false
        this.$toast('发送验证码失败，请稍后重试')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  .cross-text{
    color: #fff;
  }
  .toutiao {
    font-size: 37px;
  }

  .send-sms-btn{
    width: 162px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
    border: none;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
